package com.yu.domain;

import com.yu.mapper.PermissionDao;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Description: TODO
 * @Author : yu
 * Date : 4/24/2020 11:01 AM
 */
@ToString
@Component
public class User implements UserDetails {
    private Integer id;
    private String username;
    private String password;
    private List<Role> roles;
//    private List<SimpleGrantedAuthority> authorities = new ArrayList<>();
    private Set<SimpleGrantedAuthority> authority = new HashSet<>();
    private PermissionDao permissionDao;

    public void setAuthority(Set<SimpleGrantedAuthority> authority) {
        this.authority = authority;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
//        for (Role role : roles) {
//            authorities.add(new SimpleGrantedAuthority("ROLE_"+role.getRoleName()));
//        }
//        List<Permission> permissions = permissionDao.findByAdminUserId(id);
//        for (Role role : roles) {
//            authority.add(new SimpleGrantedAuthority(role.getName()));
//        }
        return authority;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}



